package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"hui/models"
)

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}
type HuiController struct {
	beego.Controller
}

func (c *HuiController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 5)
	var count int64
	models.DB.Model(&models.Hui{}).Count(&count)
	offset := (page - 1) * size
	var hui []models.Hui
	err := models.DB.Model(&models.Hui{}).Find(&hui).Offset(offset).Limit(size)
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功",
			Data: hui,
		}
		c.ServeJSON()
	}
}
func (c *HuiController) Up() {
	id, _ := c.GetInt("id")
	img := c.GetString("img")
	name := c.GetString("name")
	tel := c.GetString("tel")
	types := c.GetString("types")
	from := c.GetString("from")
	price, _ := c.GetFloat("price")
	status := c.GetString("status")
	time := c.GetString("time")
	hui := models.Hui{
		Id:     id,
		Img:    img,
		Name:   name,
		Tel:    tel,
		Types:  types,
		From:   from,
		Price:  price,
		Status: status,
		Time:   time,
	}
	err := models.DB.Model(&models.Hui{}).Updates(&hui).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "修改失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "修改成功",
			Data: hui,
		}
		c.ServeJSON()
	}
}
